'use strict';
const db = uniCloud.database()
const role = 0;
exports.main = async (event, context) => {
	console.log(event);
	//1.执行鉴权 获得标准响应体 和 调用接口的用户资料
	let {
		res,
		user
	} = await cheak(event);
	//2.响应体 code<=1111时，执行客户端的请求。否则直接返回获得的 响应体；
	if (res.code <= 1111) {
		event.data.data.user_name = user.name
		const disinfect = await db.collection('disinfect').add(event.data.data)
		res.data = disinfect.data

	}
	return res
}

async function cheak(e) {
	var res = {
		"code": "0000",
		"data": {}
	}
	var user = await db.collection('user').where({
		'_id': e.uid,
		'token': e.token
	}).get()
	user = user.data[0];
	if (user) { //登录正常
		/*校验token 是否过期；是否失效 ----暂不开发后续云函数支持互调后开发
		if(user.timestamp - new Date().getTime()>3600){
			res.token = md5(new Date().getTime());
			res.code  = "1111";
			......
		}*/
		if (role != user.role) {
			//res.code = 4003
		}
	} else {
		res.code = 4001;
	}
	return {
		res,
		user
	}
}
